package com.itheima.reggie.service.impl;

import com.itheima.reggie.dao.*;
import com.itheima.reggie.entity.*;
import com.itheima.reggie.service.OrderService;
import com.itheima.reggie.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.itheima.reggie.service.OrderService;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private OrderDetailDao orderDetailDao;
    @Autowired
    private ShoppingCartDao shoppingCartDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private AddressBookDao addressBookDao;

    @Override
    @Transactional
    public void add(Orders order) {
        Long userId = order.getUserId();
        // 查找订单所属的用户对象
        User user = userDao.findById(userId);
        // 根据地址的id查找地址对象
        AddressBook addressBook = addressBookDao.getById(order.getAddressBookId());
        //得到该用户所有数据
        List<ShoppingCart> shoppingCartList = shoppingCartDao.findByUserId(userId);
        // 创建一个订单保存每一个订单明细
        List<OrderDetail> orderDetailList = new ArrayList<>();
        // 生成订单id
        long orderId = UUIDUtils.getUUIDInOrderId().longValue();
        // 定义一个遍历保存总价
        BigDecimal totalAmount = new BigDecimal("0");
        //遍历购物的购物项，每一个购物项就对应订单明细
        for (ShoppingCart shoppingCart : shoppingCartList) {
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setName(shoppingCart.getName()); // 订单对应的菜品或者套餐的名字
            orderDetail.setOrderId(orderId); // 订单的id
            orderDetail.setDishId(shoppingCart.getDishId()); // 订单菜品id
            orderDetail.setSetmealId(shoppingCart.getSetmealId()); // 套餐id
            orderDetail.setDishFlavor(shoppingCart.getDishFlavor()); // 口味
            orderDetail.setNumber(shoppingCart.getNumber()); // 数量
            orderDetail.setAmount(shoppingCart.getAmount()); // 单价
            orderDetail.setImage(shoppingCart.getImage()); // 菜品或者套餐图片
            orderDetailList.add(orderDetail);

            // 计算总价
            totalAmount = totalAmount.add(shoppingCart.getAmount().multiply(new BigDecimal(shoppingCart.getNumber()+""))) ;
        }

        //构建订单
        order.setId(orderId); // 订单号
        order.setNumber(orderId+""); // 订单号
        order.setStatus(1);
        order.setOrderTime(LocalDateTime.now()); // 下单时间
        order.setAmount(totalAmount); // 总价
        order.setUserName(user.getName()); // 订单所属用户名
        order.setPhone(addressBook.getPhone()); // 收货人手机号
        order.setAddress(addressBook.getDetail()); // 收货人地址
        order.setConsignee(addressBook.getConsignee()); // 收件人姓名

        //插入订单
        orderDao.save(order);
        //插入订单明细
        orderDetailDao.batchSave(orderDetailList);

        // 清空购物车
        shoppingCartDao.deleteByUserId(userId);
    }
}
